//
//  WJRealmController.m
//  JerryIos
//
//  Created by 王军 on 2018/2/15.
//  Copyright © 2018年 王军. All rights reserved.
//

#import "WJRealmController.h"
#import "WJRealmData.h"
#import "WJRealmUtils.h"

@interface WJRealmController ()

@end

@implementation WJRealmController

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (IBAction)insertData:(id)sender {
    WJRealmData *data = [[WJRealmData alloc] init];
    data.id = [WJRealmUtils getMaxId] + 1;
    data.name = @"aaa";
    data.pwd = @"123456";
    data.age = 12;
    
    [WJRealmUtils addData:data];
}

- (IBAction)updateData:(id)sender {
    WJRealmData *data = [[WJRealmData alloc] init];
    data.id = 1;
    data.name = @"aaabbb";
    data.pwd = @"654321";
    data.age = 13;
    
    [WJRealmUtils updateData:data];
}

- (IBAction)deleteData:(id)sender {
    WJRealmData *data = [WJRealmUtils dataById:1];
    [WJRealmUtils deleteData:data];
}

- (IBAction)selectAllData:(id)sender {
    NSArray *dataArray = [WJRealmUtils data];
    for (WJRealmData *data in dataArray) {
        NSLog(@"id=%i,   name=%@,   pwd=%@,   age=%i", data.id, data.name, data.pwd,  data.age);
    }
}

- (IBAction)insertDataArray:(id)sender {
    WJRealmData *data1 = [[WJRealmData alloc] init];
    data1.id = 1;
    data1.name = @"aaa";
//    data1.pwd = @"123456";  //pwd设置了默认值
    data1.age = 12;
    
    WJRealmData *data2 = [[WJRealmData alloc] init];
    data2.id = 2;
    data2.name = @"bbb";
    data2.pwd = @"454545";
    data2.age = 22;
    
    WJRealmData *data3 = [[WJRealmData alloc] init];
    data3.id = 3;
    data3.name = @"ccc";
//    data3.pwd = @"676733";
    data3.age = 33;
    
    NSMutableArray *mutDataArray = [NSMutableArray array];
    [mutDataArray addObject:data1];
    [mutDataArray addObject:data2];
    [mutDataArray addObject:data3];
    
    [WJRealmUtils addDataArray:mutDataArray];
    
}

- (IBAction)updateDataArray:(id)sender {
    WJRealmData *data1 = [[WJRealmData alloc] init];
    data1.id = 1;
    data1.name = @"aaa444";
    data1.age = 11;
    
    WJRealmData *data2 = [[WJRealmData alloc] init];
    data2.id = 2;
    data2.name = @"bbb555";
    data2.age = 22;
    
    NSMutableArray *mutDataArray = [NSMutableArray array];
    [mutDataArray addObject:data1];
    [mutDataArray addObject:data2];
    
    [WJRealmUtils updateDataArray:mutDataArray];
}

- (IBAction)deleteDataArray:(id)sender {
    NSMutableArray *mutDataArray = [NSMutableArray array];
    [mutDataArray addObject:[WJRealmUtils dataById:1]];
    [mutDataArray addObject:[WJRealmUtils dataById:2]];
    
    [WJRealmUtils deleteDataArray:mutDataArray];
}

@end
